using MicroMsg.Common.Event;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class ChatTxtItem : BaseChatElement
	{
		private const string TAG = "ChatTxtItem";
		private const int MAX_ELEMENT_NUM = 400;
		private List<string> strList;
		private Paragraph sParagraph;
		private HyperlinkButton showAllBtn;
		private Button fromButton;
		private HttpPicItem fromIcon;
		private string lastContentCache;
		private string mStrContent;
		private AppMsgInfo mMsginfo;
		public static DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(string), typeof(ChatTxtItem), new PropertyMetadata(new PropertyChangedCallback(ChatTxtItem.OnFromChanged)));
		private bool isAppInfoInit;
		private EventWatcher mWatcher;
		public string From
		{
			get
			{
				return (string)base.GetValue(ChatTxtItem.FromProperty);
			}
			set
			{
				base.SetValue(ChatTxtItem.FromProperty, value);
			}
		}
		public ChatTxtItem()
		{
			base.add_Loaded(new RoutedEventHandler(this.Item_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Item_Unloaded));
		}
		private void Item_Unloaded(object sender, RoutedEventArgs e)
		{
			this.GetAppInfoUnit();
		}
		private void Item_Loaded(object sender, RoutedEventArgs e)
		{
			this.GetAppInfoInit();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.sParagraph = (base.GetTemplateChild("sParagraph") as Paragraph);
			this.showAllBtn = (base.GetTemplateChild("showAllBtn") as HyperlinkButton);
			this.fromButton = (base.GetTemplateChild("fromButton") as Button);
			if (this.fromButton != null)
			{
				this.fromButton.add_Click(new RoutedEventHandler(this.fromButton_Click));
			}
			this.fromIcon = (base.GetTemplateChild("picFromIcon") as HttpPicItem);
			this.RefreshContent();
			this.RefreshGlittleing();
			this.RefreshFrom();
		}
		private void RefreshFrom()
		{
			if (this.fromButton != null)
			{
				this.fromButton.set_Visibility(1);
			}
			if (this.mMsginfo != null)
			{
				if ((long)this.mMsginfo.type == 1L)
				{
					AppInfo appinfo = AppInfoMgr.getAppinfo(this.mMsginfo.appid, MMAppInfoIconType.MMAPPINFO_ICONTYPE_SD);
					if (appinfo != null && !string.IsNullOrEmpty(appinfo.AppName) && !string.IsNullOrEmpty(appinfo.AppWatermarkUrl))
					{
						this.From = strings.Con_QQComeFrom + appinfo.AppName;
						if (!string.IsNullOrEmpty(appinfo.AppName))
						{
							if (this.fromButton != null)
							{
								this.fromButton.set_Visibility(0);
							}
							if (!string.IsNullOrEmpty(appinfo.AppWatermarkUrl) && this.fromIcon != null && this.fromIcon != null)
							{
								this.fromIcon.LoadImage(appinfo.AppWatermarkUrl, true);
							}
						}
						return;
					}
					return;
				}
			}
		}
		private void fromButton_Click(object sender, RoutedEventArgs e)
		{
			string strUrl = string.Concat(new string[]
			{
				"http://rd.wechatapp.com/app/source/?appid=",
				this.mMsginfo.appid,
				";version=24;lang=",
				R.GetCurrentLanguage(),
				";devicetype=wp7"
			});
			ToolFunction.GotoWebLinkInWebView(strUrl, GetA8KeyScene.MMGETA8KEY_SCENE_UNKNOW);
		}
		private void RefreshContent()
		{
			if (this.sParagraph == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.mStrContent))
			{
				return;
			}
			if (this.mStrContent == this.lastContentCache)
			{
				return;
			}
			this.lastContentCache = this.mStrContent;
			this.sParagraph.get_Inlines().Clear();
			LongTxtCutter longTxtCutter = new LongTxtCutter(this.mStrContent);
			longTxtCutter.ProcessText();
			if (longTxtCutter.textList.get_Count() > 1)
			{
				this.strList = longTxtCutter.textList;
			}
			List<InLineResult> inLineResult = InlineElementHelper.GetInLineResult(longTxtCutter.textList.get_Item(0));
			int num = 0;
			using (List<InLineResult>.Enumerator enumerator = inLineResult.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					InLineResult current = enumerator.get_Current();
					if (current.mInline != null)
					{
						num++;
						this.sParagraph.get_Inlines().Add(current.mInline);
						if (num >= 400)
						{
							Run run = new Run();
							run.set_Text("......");
							this.sParagraph.get_Inlines().Add(run);
							break;
						}
					}
				}
			}
			if (longTxtCutter.textList.get_Count() <= 1 || this.showAllBtn == null)
			{
				this.showAllBtn.set_Visibility(1);
				return;
			}
			this.showAllBtn.set_Visibility(0);
			this.showAllBtn.set_Content(strings.Chat_LongTxt_SeeAll);
			this.showAllBtn.add_Click(new RoutedEventHandler(this.moreContentBtn_Click));
			if (this.chatMsg_0.nIsSender == 0)
			{
				this.showAllBtn.set_Foreground(new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)));
				return;
			}
			this.showAllBtn.set_Foreground(new SolidColorBrush(Color.FromArgb(255, 150, 207, 88)));
		}
		private void moreContentBtn_Click(object sender, RoutedEventArgs e)
		{
			object[] array = new object[2];
			array[0] = this.strList;
			if (this.chatMsg_0.nIsSender == 1)
			{
				array[1] = true;
			}
			else
			{
				array[1] = false;
			}
			ToolFunction.GotoPageForUserControl("/Source/UI/Chat/TooLongTxt/LongTxtShowPage.xaml", array, false);
		}
		public override void SetMsgContent(string msgContent)
		{
			this.mStrContent = msgContent;
			this.RefreshContent();
		}
		public override void SetChatMsg(ChatMsg Msg)
		{
			base.SetChatMsg(Msg);
			if (this.chatMsg_0.IsAppMsg())
			{
				this.mMsginfo = AppMsgMgr.parseAppXml(Msg.strMsg);
			}
			else
			{
				this.mMsginfo = null;
			}
			this.RefreshFrom();
		}
		private void RefreshGlittleing()
		{
			if (this.ChatContentCtrl == null)
			{
				return;
			}
			if (base.IsGlittleing)
			{
				this.ChatContentCtrl.IsGlitterFlag = true;
				return;
			}
			this.ChatContentCtrl.IsGlitterFlag = false;
		}
		protected override void GlittleingChange(bool newValue)
		{
			this.RefreshGlittleing();
		}
		private static void OnFromChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private void GetAppInfoInit()
		{
			if (this.isAppInfoInit)
			{
				return;
			}
			this.mWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GET_APPINFO_SUCCESS, this.mWatcher);
			this.isAppInfoInit = true;
		}
		private void GetAppInfoUnit()
		{
			if (!this.isAppInfoInit)
			{
				return;
			}
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GET_APPINFO_SUCCESS, this.mWatcher);
			this.isAppInfoInit = false;
		}
		private void onEventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			AppInfo appInfo = evtArgs.mObject as AppInfo;
			if (appInfo == null)
			{
				return;
			}
			if (this.mMsginfo == null)
			{
				return;
			}
			if (this.mMsginfo.appid != appInfo.AppID)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.mMsginfo.fromUserName))
			{
				return;
			}
			if (!string.IsNullOrEmpty(appInfo.AppName))
			{
				this.From = strings.Con_QQComeFrom + appInfo.AppName;
				this.fromButton.set_Visibility(0);
				if (!string.IsNullOrEmpty(appInfo.AppWatermarkUrl) && this.fromIcon != null)
				{
					this.fromIcon.LoadImage(appInfo.AppWatermarkUrl, true);
				}
			}
		}
	}
}
